package com.example.satu.quizcreator.quiz_creator;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.satu.quizcreator.quiz.Answer;
import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAnswerListItemFocusChange;

import static android.view.View.VISIBLE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE.NON_FILE;

/*import com.example.QuizCreator.Answer;
import com.example.QuizCreator.R;*/

/**
 * Created by Satu on 2014-09-10.
 */
public class AnswerAdapter extends ArrayAdapter<Answer>{
    private final LayoutInflater mInflater;
    private final Fragment parentFragment;
    public AnswerAdapter(Context context, int textViewResourceId,Fragment parentFragment) {
        super(context, textViewResourceId);
        mInflater = LayoutInflater.from(getContext());
        this.parentFragment = parentFragment;
    }


    private int lastFocussedPosition = -1;
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.answer_widget_description, null);
            holder = new ViewHolder();
            holder.answer = (EditText) convertView.findViewById(R.id.simple_answer_item);
            holder.loadObjectImageView = (ImageView) convertView.findViewById(R.id.file_loaded_view);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final Answer answer = getItem(position);
        holder.answer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if(lastFocussedPosition != position){
                    if (lastFocussedPosition != -1)
                        if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
                            ((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(false, answer);
                    if (parentFragment != null && parentFragment instanceof onAnswerListItemFocusChange)
                        ((onAnswerListItemFocusChange) parentFragment).onListItemFocusChange(true, answer);
                    lastFocussedPosition = position;
                }
                if(!hasFocus){
                    final int position = view.getId();
                    final EditText answerBody = (EditText) view;
                    getItem(position).setBody(answerBody.getText().toString());
                }
            }
        });
        holder.answer.setText(answer.getBody());
        holder.answer.setId(position);

        if(answer.getFile_type() != NON_FILE){
            switch (answer.getFile_type()){
                case IMAGE_TYPE:
                    holder.loadObjectImageView.setBackgroundResource(R.drawable.photos);
                    break;
                case VIDEO_TYPE:
                    holder.loadObjectImageView.setBackgroundResource(R.drawable.video);
                    break;
                case AUDIO_TYPE:
                    holder.loadObjectImageView.setBackgroundResource(R.drawable.kbemusedsrv);
                    break;
                default:break;
            }
            holder.loadObjectImageView.setVisibility(VISIBLE);
        }

        return convertView;
    }


    private static class ViewHolder{
        public EditText answer;
        public ImageView loadObjectImageView;
    }
}